package com.platform.manager.action.system;

import com.platform.common.logger.annotation.EnableLogger;
import com.platform.common.mybatis.Page;
import com.platform.common.mybatis.context.ContextHolder;
import com.platform.common.util.JsonUtil;
import com.platform.domain.model.Code;
import com.platform.domain.model.CodeClass;
import com.platform.domain.result.DataGirdData;
import com.platform.domain.result.ResultData;
import com.platform.manager.action.BaseAction;
import com.platform.service.CodeService;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 12-8-22
 * Time: 上午10:27
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/system")
@Service("codeAction")
@ParentPackage("jsonPackage")
public class CodeAction extends BaseAction {

    @EnableLogger
    private static Logger logger;

    @Resource
    private CodeService codeService;

    private List<Code> codeList;

    public List<Code> getCodeList() {
        return codeList;
    }

    public void setCodeList(List<Code> codeList) {
        this.codeList = codeList;
    }

    @Action(value = "queryCodeList", results = {@Result(name = "success", location = "/index.vm")})
    public String queryCodeList() {
        try {
            codeList = codeService.select(null);
            print(codeList.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 查询字典信息列表
     *
     * @return
     */
    @Action(value = "queryCodes", interceptorRefs = @InterceptorRef("pageStack"),
            results = {@Result(name = "success", type = "json", params = {"root", "dataGirdData"})})
    public String queryCodes() {
        try {
            // 查询符合条件的字典列表
            List<Code> list = codeService.select(null);
            dataGirdData = new DataGirdData();
            dataGirdData.setRows(list);
            // 获取翻页参数
            Page page = ContextHolder.getPage();
            dataGirdData.setTotal(page.getTotalResult());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 保存字典类型列表
     *
     * @return
     */
    @Action(value = "saveCodes", results = {@Result(name = "success", type = "json", params = {"root", "result"})})
    public String saveCodes() {
        try {
            if (jsonStr != null) {
                print("前台发送的JSON数据：" + jsonStr);
                Map<String, List> maps = JsonUtil.jsonToMap(jsonStr, Code.class);
                List inserted = maps.get("inserted");
                List updated = maps.get("updated");
                List deleted = maps.get("deleted");

                // 保存未插入、更新、删除成功的的用户登录名
                StringBuffer buffer = new StringBuffer();

                if (inserted != null) {
                    for(Object obj : inserted){
                        Code code = (Code) obj;
                        int resInt = codeService.insert(code);
                        if (resInt != 1) {
                            buffer.append(" [").append(code.getCodeId()).append("]");
                        }
                    }
                }

                if (updated != null) {
                    for(Object obj : updated){
                        Code code = (Code) obj;
                        int resInt = codeService.update(code);
                        if (resInt != 1) {
                            buffer.append(" [").append(code.getCodeId()).append("]");
                        }
                    }
                }

                if (deleted != null) {
                    for(Object obj : deleted){
                        Code code = (Code) obj;
                        int resInt = codeService.delete(code);
                        if (resInt != 1) {
                            buffer.append(" [").append(code.getCodeId()).append("]");
                        }
                    }
                }


                result = new ResultData();
                if (buffer.length() == 0) {
                    result.setSuccess(true);
                    result.setMessage("操作成功！");
                } else {
                    result.setSuccess(false);
                    result.setMessage("操作失败！\n以下字典操作失败！\n[字典ID]：" + buffer.toString());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

}
